package gff.actions;

import gff.bsn.CatalogosBsn;
import gff.forms.ParametrosForm;
import gff.vo.ParametrosVO;

import java.io.PrintWriter;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

public class AdmonParametrosAction extends DispatchAction {

	public ActionForward inicio(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
					throws Exception {

		log.info(" AdmonParametrosAction Metodo: inicio  ");
		ActionForward forward = new ActionForward();


		String ruta ="inicioParam";

		request.getSession().setAttribute("parametros", CatalogosBsn.getListParametros());


		forward = mapping.findForward(ruta);

		return forward;
	}

	public ActionForward modificaParam(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
					throws Exception {

		log.info(" AdmonParametrosAction Metodo: modificaParam  ");

		ParametrosForm paramForm = (ParametrosForm) form; 

		ParametrosVO param = new ParametrosVO();

		PrintWriter out = null;


		
		
		String id 		 = request.getParameter("id");
		String valor     = request.getParameter("valor");
		String nombre    = request.getParameter("nombre");
		
		param.setId(id);
		param.setValor(valor.toUpperCase());
		param.setNombre(nombre);

		out = response.getWriter();

		JSONArray jsonArray = new JSONArray();
		HashMap<String,String> hm = new HashMap<String,String>();
		request.getSession().setAttribute("parametros", CatalogosBsn.getListParametros());
		if (CatalogosBsn.updateParametros(param) != -1){

			hm.put("bandera","true");
			hm.put("valor", param.getValor());
			hm.put("id", param.getId());
			hm.put("nombre", param.getNombre());
			hm.put("resp","Se actualizo correctamente el parametro ["+param.getNombre()+"] con el valor ["+param.getValor()+"]");
			paramForm.setRespuestaDesc("Se actualizo correctamente el parametro ["+param.getNombre()+"] con el valor ["+param.getValor()+"]");
		}else{
			hm.put("bandera","false");
			hm.put("resp","No se actualizo correctamente el parametro ["+param.getNombre()+"] con el valor ["+param.getValor()+"]");	
		}
		
		CatalogosBsn.getParametros();
		
		JSONObject jsonObj = JSONObject.fromObject(hm);
		jsonArray.add(jsonObj);

		out.print(jsonArray.toString());
		out.flush();
		out.close();
		return  null;
	}

}
